package com.kuzlak.cms.security;

import cn.hutool.json.JSONUtil;
import com.kuzlak.cms.component.TokenAdminUtils;
import com.kuzlak.cms.config.ConstantHeader;
import com.kuzlak.cms.libraries.IpUtils;
import com.kuzlak.cms.module.sys.entity.SysLoginLog;
import com.kuzlak.cms.utils.RedisClient;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.authentication.UsernamePasswordAuthenticationToken;
import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.stereotype.Component;
import org.springframework.util.StringUtils;
import org.springframework.web.filter.OncePerRequestFilter;

import jakarta.annotation.Resource;
import jakarta.servlet.FilterChain;
import jakarta.servlet.ServletException;
import jakarta.servlet.http.HttpServletRequest;
import jakarta.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.Objects;

/**
 * 统一token凭据处理
 */
@Component
public class JwtAuthenticationTokenFilter extends OncePerRequestFilter {

    @Resource
    private RedisClient redisClient;
    @Autowired
    private TokenAdminUtils tokenAdminUtils;

    @Override
    protected void doFilterInternal(HttpServletRequest request, HttpServletResponse response, FilterChain filterChain) throws ServletException, IOException {
        String clientIP = IpUtils.getClientIP(request);
        ConstantHeader.setIp(clientIP);

        //获取token
        String token = request.getHeader("token");
        System.out.println("token:"+token);
        if(StringUtils.hasText(token)){
            String key="login:token:"+token;
            String json = redisClient.get(key);
            System.out.println("key:"+key);
            System.out.println("json:"+json);
            if(StringUtils.hasText(json)){
                System.out.println("json1:"+json);
                LoginUserDetails userDetails = JSONUtil.toBean(json, LoginUserDetails.class);
                System.out.println("userDetails:"+userDetails);
                if(Objects.nonNull(userDetails)){
                    System.out.println("userDetails1:"+userDetails);
                    SysLoginLog token1 = tokenAdminUtils.getToken(token);
                    System.out.println("token1:"+token1);
                    if(token1!=null) {
                        ConstantHeader.setAdminToken(token1);
                        UsernamePasswordAuthenticationToken authenticationToken = new UsernamePasswordAuthenticationToken(userDetails, null, userDetails.getAuthorities());

                        System.out.println("authenticationToken:"+authenticationToken);
                        SecurityContextHolder.getContext().setAuthentication(authenticationToken);
                    }else {

                        redisClient.del(key);
                        SecurityContextHolder.getContext().setAuthentication(null);
                    }
                }else {
                    SecurityContextHolder.getContext().setAuthentication(null);
                }
            }
        }else {
         //   SecurityContextHolder.getContext().setAuthentication(null);
        }
        //放行,后面交给Spring Security 框架
        filterChain.doFilter(request,response);
    }
}
